const app = getApp();
import util from "../../../../utils/util";
Page({
    data: {
        showCot: false,
        folderList: [],
        id: "",
        index: "",
        onload: true,

    },

    onLoad: function() {
        // 监听页面加载的生命周期函数
        util.getUrl(app.globalData.url);
    },

    onShow() {
        this.getUserFilders();
        this.setData({
            showCot: false,
            onload: true,
        });
    },

    // 获取用户全部收藏夹
    getUserFilders() {
        util.request("user.php", {
            action: "collect",
            option: "collect_list",
            user_id: ks.getStorageSync("user_id"),
            openid: ks.getStorageSync("openid"),
        }, "POST").then(res => {
            if (res.data.message == "success") {
                this.setData({
                    folderList: res.data.data,
                    onload: false,
                });
            }
        });
    },

    cancel() {
        this.setData({
            showCot: false,
        })
    },

    control(e) {
        let id = e.currentTarget.dataset.id;
        let index = e.currentTarget.dataset.index;
        this.setData({
            showCot: true,
            id: id,
            index,
        });


        util.request("share.php", {
            action: "user_encode",
            type_id: this.data.id,
            user_id: ks.getStorageSync("user_id"),
            openid: ks.getStorageSync("openid"),
        }, "POST").then(res => this.user = res.data.data.user);
    },

    delete() {
        ks.showModal({
            title: '温馨提示',
            content: '确认删除收藏夹？',
            showCancel: true,
            cancelText: '取消',
            cancelColor: '#000000',
            confirmText: '确定',
            confirmColor: '#3CC51F',
            success: (result) => {
                if (result.confirm) {
                    util.request("user.php", {
                        action: "collect",
                        option: "del_dir",
                        type_id: this.data.id,
                        user_id: ks.getStorageSync("user_id"),
                        openid: ks.getStorageSync("openid"),
                    }, "POST").then(res => {
                        this.setData({
                            showCot: false,
                        }, () => {
                            this.onShow();
                        });
                    });
                }
            },
        });
    },

    onShareAppMessage(e) {
        if (e.from == "button")
            return {
                title: this.data.folderList[this.data.index].name,
                content: "这是我收纳好的文章资讯，邀请你一起来本地宝办事查询",
                imageUrl: this.data.folderList[this.data.index].img,
                path: `/pages/mine/collect/share/share?user=${encodeURIComponent(this.user)}`
            };
    }
});